package com.fhminyi.GUI;

import com.fhminyi.util.DBUtil;

import javax.swing.*;
import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.PreparedStatement;
import java.sql.SQLException;

public class RegisterFrame extends JFrame {
    private JTextField usernameField;
    private JPasswordField passwordField;
    private JPasswordField confirmPasswordField; // 添加密码确认字段
    private JTextField emailField; // 添加邮箱字段
    private JButton registerButton;

    public RegisterFrame() {
        setTitle("用户注册");
        setSize(300, 300); // 增加窗口高度以容纳邮箱字段
        setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE);
        setLocationRelativeTo(null);

        JPanel panel = new JPanel();
        panel.setLayout(new GridLayout(5, 2)); // 修改布局以容纳邮箱字段

        JLabel usernameLabel = new JLabel("用户名:");
        usernameField = new JTextField();
        JLabel passwordLabel = new JLabel("密码:");
        passwordField = new JPasswordField();
        JLabel confirmPasswordLabel = new JLabel("确认密码:");
        confirmPasswordField = new JPasswordField();
        JLabel emailLabel = new JLabel("邮箱:"); // 添加邮箱标签
        emailField = new JTextField(); // 添加邮箱字段
        registerButton = new JButton("注册");

        panel.add(usernameLabel);
        panel.add(usernameField);
        panel.add(passwordLabel);
        panel.add(passwordField);
        panel.add(confirmPasswordLabel);
        panel.add(confirmPasswordField);
        panel.add(emailLabel); // 添加邮箱标签到面板中
        panel.add(emailField); // 添加邮箱字段到面板中
        panel.add(new JLabel()); // Empty label for spacing
        panel.add(registerButton);

        registerButton.addActionListener(new ActionListener() {
            @Override
            public void actionPerformed(ActionEvent e) {
                String username = usernameField.getText();
                String password = new String(passwordField.getPassword());
                String confirmPassword = new String(confirmPasswordField.getPassword());
                String email = emailField.getText(); // 获取邮箱输入
                if (!password.equals(confirmPassword)) {
                    JOptionPane.showMessageDialog(RegisterFrame.this, "两次输入的密码不一致", "错误", JOptionPane.ERROR_MESSAGE);
                    return;
                }
                if (registerUser(username, password, email)) { // 更新注册方法调用
                    JOptionPane.showMessageDialog(RegisterFrame.this, "注册成功: " + username);
                    dispose();
                    new LoginFrame().setVisible(true);
                } else {
                    JOptionPane.showMessageDialog(RegisterFrame.this, "注册失败", "错误", JOptionPane.ERROR_MESSAGE);
                }
            }
        });

        add(panel);
    }

    private boolean registerUser(String username, String password, String email) { // 更新方法签名
        try (Connection conn = DBUtil.getConnection()) {
            String sql = "INSERT INTO 用户 (用户名, 密码, 邮箱) VALUES (?, ?, ?)"; // 更新SQL语句
            try (PreparedStatement pstmt = conn.prepareStatement(sql)) {
                pstmt.setString(1, username);
                pstmt.setString(2, password);
                pstmt.setString(3, email); // 设置邮箱参数
                int affectedRows = pstmt.executeUpdate();
                return affectedRows > 0;
            }
        } catch (SQLException ex) {
            ex.printStackTrace();
            JOptionPane.showMessageDialog(this, "注册失败: " + ex.getMessage(), "错误", JOptionPane.ERROR_MESSAGE);
        }
        return false;
    }

    public static void main(String[] args) {
        SwingUtilities.invokeLater(new Runnable() {
            @Override
            public void run() {
                new RegisterFrame().setVisible(true);
            }
        });
    }
}